package com.zllkj.mm.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.jaeger.ninegridimageview.NineGridImageView
import com.jaeger.ninegridimageview.NineGridImageViewAdapter
import com.previewlibrary.GPreviewBuilder
import com.zllkj.mm.R
import com.zllkj.mm.adapter.NearByFriendPostAdapter.PostViewHolder
import com.zllkj.mm.model.entity.GridImageViewBean
import com.zllkj.mm.module.shopping.vm.ItemFriendListVM
import com.zllkj.mm.util.common.CenterImage
import com.zllkj.mm.util.common.FormatUtil
import com.zllkj.mm.util.common.PictureLoadUtil
import com.zllkj.mm.util.common.PictureLoadUtil.Companion.loadPic
import kotlinx.android.synthetic.main.item_friend_list_data.view.*

/**
 * Created by Jaeger on 16/2/24.
 *
 * Email: chjie.jaeger@gmail.com
 * GitHub: https://github.com/laobie
 */
class NearByFriendPostAdapter(context: Context?, private val mPostList: List<ItemFriendListVM>) : RecyclerView.Adapter<PostViewHolder>() {
    private val mInflater: LayoutInflater
    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        holder.bind(mPostList[position])
    }
    override fun getItemCount(): Int {
        return mPostList.size
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        return PostViewHolder(mInflater.inflate(R.layout.item_friend_list_data, parent, false))
    }

    inner class PostViewHolder(val itemView: View) : RecyclerView.ViewHolder(itemView) {

 //       private val night_image_view: NineGridImageView<GridImageViewBean> = itemView.findViewById(R.id.ngl_images)

        private val mAdapter: NineGridImageViewAdapter<GridImageViewBean> = object : NineGridImageViewAdapter<GridImageViewBean>() {
            override fun onDisplayImage(context: Context, imageView: ImageView, s: GridImageViewBean) {
                if (s.videoUrl != null) {
                    var image = imageView as CenterImage
                    image.setCenterImgShow(true)
                }
                loadPic(imageView, s.url!!)
            }

            override fun generateImageView(context: Context): ImageView {
                var imageView = CenterImage(context)
                imageView.setCenterImgShow(false)
                return imageView
            }

            override fun onItemImageClick(context: Context, imageView: ImageView, index: Int, list: List<GridImageViewBean>) {}
        }

        fun bind(vm: ItemFriendListVM) {
         //   night_image_view.setImagesData(vm.imageList)

            var ivHead = itemView.findViewById<AppCompatImageView>(R.id.ivhead)
            loadPic(ivHead,vm.userDyBean!!.userLogo)
            vm?.userDyBean?.let {
                itemView.tvUserName.text = it.nikeName
                itemView.tv_item_friend_content.text = it.content
                itemView.tv_item_friend_comment_num.text = it.commentNum.toString()
                itemView.tv_item_friend_like_num.text = it.likeNum.toString()
                itemView.tv_age_and_sex.text = if(it.sex == 1)"男" else "女"  + "|" + it?.province + it.city + it.district
                if (it.videoUrl !=null)


                it.price?.let { price ->
                    itemView.ll_invite_time_area.visibility = View.VISIBLE
                    itemView.tv_friend_invite_money.text = FormatUtil.bigDecimalUtilFormat(price,"100") + "/次"
                    itemView.tv_friend_invite_time.text = FormatUtil.dataFormat(it.createTime)
                }
                itemView.tv_item_friend_like_num.text = it.likeNum.toString()
            }
        }
//        init {
//            night_image_view.setAdapter(mAdapter)
//            night_image_view.setItemImageClickListener { context, imageView, index, list ->
//                GPreviewBuilder.from((context as Activity))
//                        .setData(list)
//                        .setIsScale(true)
//                        .setCurrentIndex(index)
//                        .setType(GPreviewBuilder.IndicatorType.Dot)
//                        .start() //启动
//            }
//            night_image_view.setOnClickListener { }
//        }
    }

    init {
        mInflater = LayoutInflater.from(context)
    }
}